home *** CD-ROM | disk | FTP | other *** search
-
-
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <OSUtils.h>
- #include <ToolUtils.h>
- #include <SegLoad.h>
- #include <Sound.h>
-
- #include "DrawSprocket.h"
- #include "agl.h"
-
- #include <math.h>
-
- #define SCREEN_WIDTH 400
- #define SCREEN_HEIGHT 400
-
- /* globals */
- DSpContextAttributes gTheContextAttributes;
- DSpContextReference gTheContext;
-
- /* static function prototypes */
- static CGrafPtr SetupScreen(void);
- static void ShutdownScreen(CGrafPtr theFrontBuffer);
- static AGLContext setupAGL(AGLDrawable win);
- static void cleanupAGL(AGLContext ctx);
- static void drawGL(AGLContext ctx);
-
- /* source code */
- void main(void)
- {
- CGrafPtr theScreen;
- AGLContext ctx;
-
- /*******/
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- HideCursor();
-
- theScreen = SetupScreen();
-
- /* Setup the OpenGL context */
- ctx = setupAGL((AGLDrawable) theScreen);
- if(!ctx) return;
-
- do {
- /* Do the OpenGL drawing */
- drawGL(ctx);
- } while (!Button());
-
- /* Cleanup the OpenGL context */
- cleanupAGL(ctx);
-
- ShutdownScreen(theScreen);
-
- ShowCursor();
-
- FlushEvents(everyEvent, 0);
-
- ExitToShell();
- }
-
-
- static CGrafPtr SetupScreen(void)
- {
- OSStatus theError;
-
- CGrafPtr theFrontBuffer;
-
- theError = DSpStartup();
- if( theError )
- DebugStr("\pUnable to startup\n");
-
- gTheContextAttributes.displayWidth = SCREEN_WIDTH;
- gTheContextAttributes.displayHeight = SCREEN_HEIGHT;
- gTheContextAttributes.colorNeeds = kDSpColorNeeds_Require;
- gTheContextAttributes.displayDepthMask = kDSpDepthMask_16;
- gTheContextAttributes.displayBestDepth = 16;
- gTheContextAttributes.pageCount = 1;
-
- theError = DSpFindBestContext( &gTheContextAttributes, &gTheContext );
- if( theError )
- DebugStr("\pUnable to find a suitable device\n");
-
- theError = DSpContext_Reserve( gTheContext, &gTheContextAttributes );
- if( theError )
- DebugStr("\pUnable to create the display!");
-
- theError = DSpContext_FadeGammaOut( NULL, NULL );
- if( theError )
- DebugStr("\pUnable to fade the display!");
-
- theError = DSpContext_SetState( gTheContext, kDSpContextState_Active );
- if( theError )
- DebugStr("\pUnable to set the display!");
-
- theError = DSpContext_FadeGammaIn( NULL, NULL );
- if( theError )
- DebugStr("\pUnable to fade the display!");
-
- {
- // create a window to draw into
- Rect r;
- AuxWinHandle awh;
- r.top = r.left = 0;
- DSpContext_LocalToGlobal(gTheContext, (Point *)&r);
- r.right = r.left + SCREEN_WIDTH;
- r.bottom = r.top + SCREEN_HEIGHT;
-
- theFrontBuffer = (CGrafPtr)NewCWindow (NULL, &r, "\p", 0, plainDBox, (WindowPtr)-1, 0, 0);
-
- // set the content color of the window to black to avoid a white flash when the window appears.
- if(GetAuxWin ((WindowPtr)theFrontBuffer, &awh)){
- CTabHandle theColorTable;
- OSErr err;
-
- /*****/
-
- theColorTable = (**awh).awCTable;
- err = HandToHand((Handle*)&theColorTable);
- if(err)
- DebugStr("\pOut of memory!");
-
- (**theColorTable).ctTable[wContentColor].rgb.red = 0;
- (**theColorTable).ctTable[wContentColor].rgb.green = 0;
- (**theColorTable).ctTable[wContentColor].rgb.blue = 0;
-
- CTabChanged(theColorTable);
-
- // the color table will be disposed by the window manager when the window is disposed
- SetWinColor((WindowPtr)theFrontBuffer, (WCTabHandle)theColorTable);
-
- }
- ShowWindow((GrafPtr)theFrontBuffer);
- SetPort((GrafPtr)theFrontBuffer);
-
- {
- RGBColor b = { 0xFFFF, 0xFFFF, 0xFFFF };
- RGBColor f = { 0x0000, 0x0000, 0x0000 };
-
- RGBForeColor(&f);
- RGBBackColor(&b);
- }
- }
- return theFrontBuffer;
- }
-
- static void ShutdownScreen(CGrafPtr theFrontBuffer)
- {
- DSpContext_FadeGammaOut( NULL, NULL );
- DisposeWindow((WindowPtr)theFrontBuffer);
- DSpContext_SetState( gTheContext, kDSpContextState_Inactive );
- DSpContext_FadeGammaIn( NULL, NULL );
- DSpContext_Release( gTheContext );
- DSpShutdown();
- }
-
- /*
- ** OpenGL Setup
- */
- static AGLContext setupAGL(AGLDrawable win)
- {
- GLint attrib[] = { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_NONE };
- AGLPixelFormat fmt;
- AGLContext ctx;
- GLboolean ok;
-
- /* Choose an rgb pixel format */
- fmt = aglChoosePixelFormat(NULL, 0, attrib);
- if(fmt == NULL) return NULL;
-
- /* Create an AGL context */
- ctx = aglCreateContext(fmt, NULL);
- if(ctx == NULL) return NULL;
-
- /* Attach the window to the context */
- ok = aglSetDrawable(ctx, win);
- if(!ok) return NULL;
-
- /* Make the context the current context */
- ok = aglSetCurrentContext(ctx);
- if(!ok) return NULL;
-
- /* Pixel format is no longer needed */
- aglDestroyPixelFormat(fmt);
-
- return ctx;
- }
-
- /*
- ** OpenGL Cleanup
- */
- static void cleanupAGL(AGLContext ctx)
- {
- aglSetCurrentContext(NULL);
- aglSetDrawable(ctx, NULL);
- aglDestroyContext(ctx);
- }
-
- /*
- ** OpenGL Drawing
- */
- static void drawGL(AGLContext ctx)
- {
- static float f, s, c;
-
- f += 0.01;
- s = sin(f);
- c = cos(f);
-
- /* Clear color buffer to yellow */
- glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
-
- /* Draw a smooth shaded polygon */
- glBegin(GL_POLYGON);
- glColor3d(1.0, 0.0, 0.0);
- glVertex3d(s, c, 0.0);
- glColor3d(0.0, 1.0, 0.0);
- glVertex3d(-c, s, 0.0);
- glColor3d(0.0, 0.0, 1.0);
- glVertex3d(-s, -c, 0.0);
- glColor3d(1.0, 0.0, 1.0);
- glVertex3d(c, -s, 0.0);
- glEnd();
-
- /* Ensure completion */
- //glFinish();
- aglSwapBuffers(ctx);
- }
-